<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据编号" prop="businessNum" v-show="showSearch">
        <el-input
          v-model="queryParams.businessNum"
          placeholder="请输入单据编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table  :data="reportList" >
      <el-table-column label="ID" align="center" prop="id" min-width="80" />
      <el-table-column label="报损单号" align="center" prop="businessNum" min-width="180" />
      <el-table-column label="园区" align="center" prop="area" width="120" />
      <el-table-column label="盘点时间" align="center" prop="storeTime" width="180">
      </el-table-column>
      <el-table-column label="盘点人" prop="createBy" width="160" align="center"/>
      <el-table-column label="备注" prop="remark" min-width="160" align="center">
        <template #default="scope">
          <span v-show="!scope.row.editFlag" >{{scope.row.remark}}</span>
          <el-input v-show="scope.row.editFlag" v-model="scope.row.remark" placeholder="请输入备注"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="{row}">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template v-slot="{row}">
          <el-button size="small"
                     type="text"
                     icon="CircleCheck"
                     v-show="row.editFlag"
                     @click="submit(row)">保存
          </el-button>
          <el-button size="small"
                     type="text"
                     icon="CircleClose"
                     v-show="row.editFlag"
                     @click="cancel(row)">取消
          </el-button>
          <el-button v-show="!row.editFlag" size="mini" type="text" icon="el-icon-edit" @click="edit(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(row)">明细</el-button>
          <el-button type="text"  icon="el-icon-download" size="mini" @click="rowExport(row)">导出</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"
    />
  </div>
</template>

<script>
import {foodStoreList} from "@/api/store/resource";
import {getDicts} from "@/api/system/dict/data";
import {overflowReportList, overflowReportUpdate} from "@/api/store/calculate/calculate";

export default {
  name: "Losses",
  dicts:['user_area'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      reportList:[],
      // 食材表格数据
      queryParams: {
        type:'报损单',
        pageNum: 1,
        pageSize: 10,
        businessNum: '',
        area:'',
      },
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
    // this.getDictData()
  },
  methods: {
    /** 查询列表 */
    getList() {
      overflowReportList(this.queryParams).then(res => {
        res.rows.map(item=>{
          item.editFlag = false;
        })
        this.reportList=res.rows
        this.total = res.total;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    getDictData(){
      getDicts('food_unit').then(res=>{
        this.foodUnit=res.data
      })
    },
    handleDetail(row){
      // console.log(row.businessNum)
      this.$router.push({
        path:"/store/losses/detail/",
        query:{
          businessNum:row.businessNum
        }
      });
    },
    edit(row,index){
      row.editFlag = true;
      // this.$set(this.reportList, index, row);
    },
    submit(row){
      let updateData = {
        id: row.id,
        remark: row.remark,
      }
      overflowReportUpdate(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel(row){
      row.editFlag = false;
      this.getList();
    },
    //行导出
    rowExport(row){
      this.$router.push({
        path:'/jmreport/losses',
        query:{
          id:'903478200944603136',
          rowId:row.businessNum
        }
      })
    },
  }
};
</script>
